local haoduo = fk.CreateSkill {
  name = "th_haoduo",
}

haoduo:addEffect(fk.EventPhaseProceeding, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(haoduo.name) and player.phase == Player.Draw
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local players = table.filter(room:getOtherPlayers(player, false), function(p) return p:getHandcardNum() >= 2 end)
    local from = room:askToChoosePlayers(player, {
      targets = players,
      min_num = 1,
      max_num = 1,
      prompt = "#th_haoduo-choose",
      skill_name = haoduo.name,
      cancelable = true
    })
    if #from == 1 then
      event:setCostData(self, { tos = from })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost = event:getCostData(self)
    player.room:notifySkillInvoked(player, haoduo.name)
    local to = cost.tos[1]
    local cards = room:askToChooseCards(player, {
      target = to,
      min = 2,
      max = 2,
      flag = "h",
      skill_name = haoduo.name
    })
    room:obtainCard(player, cards, false, fk.ReasonPrey)
    room:setPlayerMark(player, "@[chara]th_haoduo-turn", to.id)
  end,
})

haoduo:addEffect(fk.EventPhaseEnd, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    if target ~= player or player.phase ~= Player.Play or player:getMark("@[chara]th_haoduo-turn") == 0 then return end
    local to = player.room:getPlayerById(player:getMark("@[chara]th_haoduo-turn"))
    return to and to:isAlive() and not player:isKongcheng()
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = player.room:getPlayerById(player:getMark("@[chara]th_haoduo-turn"))
    local cards = player:getCardIds("h")
    if #cards > 2 then
      cards = room:askToCards(player, {
        min_num = 2,
        max_num = 2,
        include_equip = false,
        skill_name = "th_haoduo",
        cancelable = false,
        prompt = "#th_haoduo-give:" .. to.id
      })
    end
    room:setPlayerMark(player, "@[chara]th_haoduo-turn", 0)
    room:obtainCard(to, cards, false, fk.ReasonGive)
  end,
})

Fk:loadTranslationTable{
  ["th_haoduo"] = "豪夺",
  [":th_haoduo"] = "摸牌阶段，你可以获得一名其他角色两张手牌，本回合出牌阶段结束时，你交给其等量张手牌（不足则全给）。",

  ["#th_haoduo-choose"] = "豪夺：你可以获得一名其他角色两张手牌，本回合出牌阶段结束时，你交给其等量张手牌",
  ["#th_haoduo-give"] = "豪夺：你须交给 %src 两张手牌。",

  ["@[chara]th_haoduo-turn"] = "豪夺",
}

return haoduo
